package com.sixth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BusCarDao;
import com.sixth.entity.BusCar;
import com.sixth.entity.User;
import com.sixth.service.BuscarService1;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: carrent
 * @description:
 * @author: 老范
 * @create: 2022-01-13 21:50
 **/
@Service(value = "buscarService1")
public class BuscarServicelmpl1 implements BuscarService1 {
    @Autowired
    private BusCarDao busCarDao;
    public TableLayUi listByCondition(int page, int limit, String carnumber, String cartype, String color, Double price, Double rentprice , Double deposit, Integer isrenting,String description,String carimg) {
        PageHelper.startPage(page,limit);
        List<BusCar> busCars=busCarDao.listByConditioner(carnumber,cartype,color,price,rentprice,deposit,isrenting,description,carimg);
        PageInfo<BusCar> pageInfo = new PageInfo<BusCar>(busCars);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    public CommonResult delete(String carnumber){
        Integer i=busCarDao.deleteByPrimaryKey(carnumber);
        CommonResult commonResult;
        if (i > 0) {
            commonResult = CommonResult.success(null, "删除成功");
        } else {
            commonResult = CommonResult.fail("删除失败");
        }

        return commonResult;
    }

    public CommonResult add(String carnumber, String cartype, String color, Double price, Double rentprice, Double deposit, Integer isrenting,String description,String carimg, Date createtime) {
        //获取当前时间
        Date date = new Date();
        createtime=date;
        int i = busCarDao.add(carnumber,cartype,color,price,rentprice,deposit,isrenting,description,carimg, createtime);
        if (i > 0) {
            return new CommonResult(1,"操作成功",null);
        }
        return new CommonResult(2,"操作成功",null);
    }

    public CommonResult update(String carnumber, String cartype, String color, Double price, Double rentprice, Double deposit, Integer isrenting, String description,String carimg, Date createtime) {
        //获取当前时间
        Date date = new Date();
        createtime=date;
        int i = busCarDao.update(carnumber,cartype,color,price,rentprice,deposit,isrenting,description,carimg, createtime);
        if (i > 0) {
            return new CommonResult(1,"操作成功",null);
        }
        return new CommonResult(2,"操作成功",null);
    }

    public TableLayUi selectone(String carnumber,String carimg){
        List<BusCar> busCars1=busCarDao.selectone(carnumber,carimg);
        PageInfo<BusCar> pageInfo=new PageInfo<BusCar>(busCars1);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }
    //批量删除
    public CommonResult batch(String carnumber) {
        List<BusCar> busCars = JSONArray.parseArray(carnumber, BusCar.class);
        BusCar u = new BusCar();
        for (int i = 0; i < busCars.size(); i++) {
            u.setCarnumber(busCars.get(i).getCarnumber());
            String uid = u.getCarnumber();
            int j = busCarDao.dels(uid);
            if (j == 0) {
                return new CommonResult(5000, "操作失败", null);
            }
        }
        return new CommonResult(2000,"操作成功",null);
    }
}
